﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Medical.DataInterface;
using Medical.Entity;

namespace Medical.Data
{
    /// <summary>
    /// 管理员类型信息数据操作类
    /// </summary>
    public sealed class AdminTypeAccessor : IAdminTypeAccessor
    {
        #region IBaseAccessor<AdminType> 成员
        /// <summary>
        /// 获取管理员类型
        /// </summary>
        /// <param name="nID"></param>
        /// <returns></returns>
        public AdminType Get(int nID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [AdminType] where [ID]=@ID",
                                                         new SqlParameter("@ID", nID)
                                                         );
            AdminType Info = null;
            while (dr.Read())
            {
                Info = new AdminType();
                Info.ID = Convert.ToInt32(dr["ID"]);
                Info.Name = dr["Name"].ToString();
                Info.Features = (long)(dr["Features"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 获取权限值列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAdminFeatures()
        {
            return SqlHelper.ExecuteDataset(
                     SqlHelper.database,
                     CommandType.Text,
                     "select * from [AdminFeatures] order by [ParentID],[OrderNum]").Tables[0];
        }

        /// <summary>
        /// 获取管理员权限组列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList()
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select * from [AdminType] order by [ID] desc").Tables[0];
        }

        /// <summary>
        /// 新增管理员类型
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Insert(AdminType Info)
        {
            string strSql = "insert into [AdminType]([Name],[Features]) values(@Name,@Features)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@Features", Info.Features));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 编辑管理员类型
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Update(AdminType Info)
        {
            string strSql = "update [AdminType] set [Name]=@Name,[Features]=@Features where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@Features", Info.Features));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 删除管理员类型
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool Delete(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "delete from [AdminInfo] where [AdminTypeID]=@ID;delete from [AdminType] where [ID]=@ID;",
                   new SqlParameter("@ID", nID)) > 0;
        }

        #endregion
    }
}
